package com.chen.gulimall.ware.service.impl;

import com.chen.gulimall.ware.entity.PurchaseDetailEntity;
import com.chen.gulimall.ware.service.PurchaseDetailService;
import com.chen.gulimall.ware.service.WareSkuService;
import com.chen.gulimall.ware.vo.MergeVo;
import com.chen.gulimall.ware.vo.PurchaseDoneVo;
import com.chen.gulimall.ware.vo.PurchaseItemDoneVo;
import com.chen.gulimallcommon.common.constant.WareConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.gulimallcommon.common.utils.PageUtils;
import com.chen.gulimallcommon.common.utils.Query;

import com.chen.gulimall.ware.dao.PurchaseDao;
import com.chen.gulimall.ware.entity.PurchaseEntity;
import com.chen.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;

    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);
    }


    @Transactional
    @Override
    public void updateMerge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();

        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatus.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());

            this.save(purchaseEntity);
            //mybatis有一个参数，可以在保存的时候将主键顺便赋值给属性
            purchaseId = purchaseEntity.getId();
        }

        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> list = mergeVo.getItems().stream().map(item -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(item);
            detailEntity.setPurchaseId(finalPurchaseId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatus.ASSIGNED.getCode());
            return detailEntity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(list);

        //执行完成之后更新采购单的更新时间
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());


    }

    @Override
    public void updatePurchaseStatus(List<Long> ids) {
        List<PurchaseEntity> list = ids.stream().map(id-> {
            PurchaseEntity purchaseEntity = this.getById(id);
            return purchaseEntity;
        }).filter(purchaseEntity -> {
            if (purchaseEntity.getStatus() == WareConstant.PurchaseStatus.CREATED.getCode() || purchaseEntity.getStatus() == WareConstant.PurchaseStatus.ASSIGNED.getCode()) {
                return true;
            }

            return false;
        }).map(item -> {
            item.setStatus(WareConstant.PurchaseStatus.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(list);

        //此次更新采购需求中的状态
        list.forEach(item->{
            List<PurchaseDetailEntity> detailEntities = purchaseDetailService.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id",item.getId()));
            List<PurchaseDetailEntity> entities = detailEntities.stream().map(detailEntity-> {
                PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                purchaseDetailEntity.setId(detailEntity.getId());
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatus.BUYING.getCode());
                return purchaseDetailEntity;
            }).collect(Collectors.toList());

            purchaseDetailService.updateBatchById(entities);
        });
    }

    @Override
    public void done(PurchaseDoneVo doneVo) {

        //修改采购项的内容
        List<PurchaseItemDoneVo> items = doneVo.getItems();
        List<PurchaseDetailEntity> detailEntities = new ArrayList<>();
        boolean flag = true;
        for (PurchaseItemDoneVo item : items) {
            PurchaseDetailEntity entity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.PurchaseDetailStatus.HASHRROR.getCode()) {
                flag = false;
                entity.setStatus(item.getStatus());
            } else {
                entity.setStatus(WareConstant.PurchaseDetailStatus.FINISH.getCode());

                //更新仓库内容
                PurchaseDetailEntity purchaseDetailEntity = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(purchaseDetailEntity.getSkuId(),purchaseDetailEntity.getWareId(),purchaseDetailEntity.getSkuNum());

            }

            entity.setId(item.getItemId());

            detailEntities.add(entity);
        }

        purchaseDetailService.updateBatchById(detailEntities);

        //更新采购单的内容
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(doneVo.getId());
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setStatus(flag? WareConstant.PurchaseStatus.FINISH.getCode() : WareConstant.PurchaseStatus.HASHRROR.getCode());

        this.updateById(purchaseEntity);
    }

}